home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 55308 / 55308.xpi / chrome / goo.gl_lite.jar / content / goo.gl_lite.js next >
Encoding:
Text File  |  2010-01-27  |  3.5 KB  |  101 lines

  1. /*
  2.     goo.gl lite, url shortening without the extra weight.
  3.     Copyright (C) 2009 Matthew Flaschen
  4.  
  5.     This program is free software; you can redistribute it and/or modify
  6.     it under the terms of the GNU General Public License as published by
  7.     the Free Software Foundation; either version 2 of the License, or
  8.     (at your option) any later version.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License along
  16.     with this program; if not, write to the Free Software Foundation, Inc.,
  17.     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. */
  19.  
  20. goo_gl_lite = new function()
  21. {
  22.     const gClipboardHelper = Components.classes["@mozilla.org/widget/clipboardhelper;1"].
  23.     getService(Components.interfaces.nsIClipboardHelper);
  24.  
  25.     const notificationValue = "goo.gl lite notification";
  26.     const iconURL = "chrome://goo.gl_lite/skin/icon_16x16.png";
  27.  
  28.     /**
  29.      * Basic initiation
  30.      */
  31.     this.init = function()
  32.     {
  33.         document.getElementById("contentAreaContextMenu").addEventListener("popupshowing", goo_gl_lite.popupshowing, false);
  34.     };
  35.  
  36.     /**
  37.      * Show/hide context menu entries on demand
  38.      */
  39.     this.popupshowing = function()
  40.     {
  41.         gContextMenu.showItem("context-goo_gl_lite-current", !(gContextMenu.isContentSelected || gContextMenu.onTextInput || gContextMenu.onLink || gContextMenu.onImage || gContextMenu.onVideo || gContextMenu.onAudio)); // Shows Copy Goo.gl URL for This Page whenever Bookmark This Page is shown
  42.         gContextMenu.showItem("context-goo_gl_lite-link", gContextMenu.onLink && !gContextMenu.onMailtoLink); // Shows Copy Goo.gl URL for Link Location whenver Bookmark this Link is shown.
  43.     };
  44.  
  45.     /**
  46.      * Makes a short url from long_url
  47.      * @param long_url long url, unescaped.
  48.      */
  49.     this.make_short_url = function(long_url)
  50.     {
  51.         var req = new XMLHttpRequest();
  52.         req.addEventListener("load", function()
  53.         {
  54.             var response = JSON.parse(req.responseText);
  55.             if(response.error_message)
  56.             {
  57.                 goo_gl_lite.error("Gateway returned error message: " + response.error_message);
  58.             }
  59.             goo_gl_lite.notify(response.short_url + " has been copied to the clipboard.  Shortened from " + long_url, "PRIORITY_INFO_MEDIUM");
  60.             gClipboardHelper.copyString(response.short_url);
  61.         }, false);
  62.         req.addEventListener("error", function()
  63.         {
  64.             goo_gl_lite.error("Error contacting gateway.  Status code: " + req.status);
  65.         }, false);
  66.         req.open("GET", "http://ggl-shortener.appspot.com/?url=" +
  67.              encodeURIComponent(long_url));
  68.  
  69.         req.send();
  70.     };
  71.  
  72.     /**
  73.      * @param text Text of notification
  74.      * @param priorityKey key to specify priority, as string
  75.      */
  76.     this.notify = function(text, priorityKey)
  77.     {
  78.         var notifyBox = window.getNotificationBox(top.getBrowser().selectedBrowser.contentWindow);
  79.         notifyBox.removeAllNotifications(false);
  80.         notifyBox.appendNotification("Goo.gl Lite: " + text, this.notificationValue, this.iconURL, notifyBox[priorityKey], null);
  81.     };
  82.  
  83.     this.error = function(error_text)
  84.     {
  85.         this.notify("Short URL creation failed: " + error_text, "PRIORITY_WARNING_MEDIUM");
  86.         throw new Error("[goo.gl lite] Short URL creation failed: " + error_text);
  87.     };
  88.  
  89.     this.make_from_current_page = function()
  90.     {
  91.         this.make_short_url(top.getBrowser().currentURI.spec);
  92.     };
  93.  
  94.     this.make_from_link = function()
  95.     {
  96.         this.make_short_url(gContextMenu.linkURL);
  97.     };
  98. }();
  99.  
  100. window.addEventListener("load", goo_gl_lite.init, false);
  101.